/*
 * BattleState.cpp
 *
 *  Created on: 16-02-2012
 *      Author: glu
 */

#include "BattleState.h"

BattleState::BattleState() {
	nextState = NoState;

	background = NULL;
	tile = NULL;
	tile_range = NULL;

	/* TODO
	 * TU SIE SYPIE GOWNO JEDNO
	 */
//	Global::backgroundGlobal = Global::loadImage("background.jpg");
	background = Global::loadImage("background.jpg");
//	tile = Global::loadImage("/home/glu/Programowanie/BattleOfHeroes/tile.png");
//	tile_range = Global::loadImage("range.png");

	if (background == NULL)
		std::cout<<"K: Ni ma tla\n";
	if (tile == NULL)
		std::cout<<"K: Ni ma plytki\n";
	if (tile_range == NULL)
		std::cout<<"K: Ni ma zasiegu\n";

	tiles_grid = new Tile* [TILES_ROWS_COUNT];

	for (int i=0; i <TILES_ROWS_COUNT ; ++i){
		tiles_grid[i] = new Tile[TILES_COLUMNS_COUNT];
		for (int j=0; j<TILES_COLUMNS_COUNT; j++){
			int posX = i%2 ? ((j+1)+0.5)*TILE_WIDTH : (j+1)*TILE_WIDTH;
			int posY = (i+1)*TILE_HEIGHT;
			tiles_grid[i][j] = Tile(posX, posY);
		}
	}

	Global::applySurface(0, 0, background, Global::screen);

}

BattleState::~BattleState() {
	SDL_FreeSurface(background);
	SDL_FreeSurface(tile);
	SDL_FreeSurface(tile_range);

	for (int i=0; i <TILES_ROWS_COUNT ; ++i){
		delete [] tiles_grid[i];
	}
	delete tiles_grid;
}


void BattleState::handleEvents( SDL_Event &event ){
	switch( event.type ){
	case SDL_MOUSEBUTTONDOWN: {
		if( event.button.button == SDL_BUTTON_LEFT ){
			/* TODO
			 * Obsluga zdarzen!!!
			 * Tu nie wolno aktualizowac ekranu.
			 * Tutaj nalezy wykonac zaznaczenie plytki (przy ruchu postaci)
			 * Przeglada sie liste potworow, czy przypadkiem nie kliknelo sie
			 * w niego. Jesli tak, to zaznacza sie plytke (A nie odwrotnie?! Łatwiej...)
			 */

			for ( int i=0; i<TILES_ROWS_COUNT; i++ ){
				for ( int j=0; j<TILES_COLUMNS_COUNT; j++ ){
					tiles_grid[i][j].setInRange(false);
					tiles_grid[i][j].setActive(false);
				}
			}

			int i = event.button.y / TILE_HEIGHT;
			int j =  i%2 ? event.button.x/TILE_WIDTH : (event.button.x-TILE_WIDTH/2)/TILE_WIDTH;

			if ( i>0 && j>0 && i<=TILES_ROWS_COUNT && j<=TILES_COLUMNS_COUNT ){
				--i;
				--j;
				tiles_grid[i][j].setInRange(true);
				tiles_grid[i][j].setActive(true);
			}

			std::cerr << event.button.x << " " << event.button.y << std::endl;

		}
		break;
	}
	case SDL_KEYDOWN: {
		if (event.key.keysym.sym == SDLK_RETURN )
			nextState = ExitState;
		break;
	}
	case SDL_QUIT: {
		nextState = ExitState;
		break;
	}
	default: {break;}
	}
}

void BattleState::logic(){

}

void BattleState::update(){
	//	Rysowanie powinno uwzgledniac zasieg przemieszczania potworka
	//	Potworek powinien zaznaczac pola, ktora sa aktywne podczas ruchu!

	for(int i=0; i<TILES_ROWS_COUNT; i++){
		for(int j=0; j<TILES_COLUMNS_COUNT; j++){
			if(tiles_grid[i][j].getActive() == true)
				applyTileSurface( tiles_grid[i][j], tile_range, Global::screen);
			else
				applyTileSurface( tiles_grid[i][j], tile, Global::screen);
		}
	}
}

GameStates BattleState::getNextState(){
	return nextState;
}

void BattleState::applyTileSurface( Tile tile, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clip){
	SDL_Rect rectOffset = tile.getBounds();

	SDL_BlitSurface( src, clip, dst, &rectOffset );
}
